﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using WebMvc.Areas.Webmonitor.Models;
using WebMvc.Controllers;
using WebMvc.Models;

namespace WebMvc.Areas.Webmonitor.Controllers
{
    [Area("Webmonitor")]
    public class InfoHitController : BaseController<InfoHit>
    {
        public InfoHitController(EFDbContext context) : base(context)
        {
        }
        public override void SetOrderLambda()    //可以在子类中覆盖此方法，必须是实体中存在的(参考实体定义)，否则出错,例如Id，区分大小写
        {
            OrderLambda = o => o.Id;
        }
        public override void SetIsASC()  //可以在子类中覆盖此方法，设置isASC为false则按降序排
        {
            IsASC = false;
        }

        #region Page
        WebsiteTypeController _wts = new WebsiteTypeController();
        WebsiteController _ws = new WebsiteController();

        //获取以网站类型为分类的信息
        public IActionResult GetWebsiteTypeNews(int num = 10)
        {
            var websiteTypeList = _wts.GetAllListToJson().ToList();
            ViewBag.websiteTypeList = websiteTypeList;

            List<InfoHit> infohit = new List<InfoHit>(websiteTypeList.Count * num);
            foreach (WebsiteType websiteType in websiteTypeList)
            {
                var temp = GetEntitysByLambdaAndSort(o => o.WebsiteTypeId == websiteType.Id, OrderLambda, IsASC).Take(num).ToList();
                infohit.AddRange(temp);
            }
            return View(infohit);
        }

        //获取最近一小时信息
        public IActionResult GetWebsiteTypeNewsForOneHour()
        {
            var websiteTypeList = _wts.GetAllListToJson().ToList();
            ViewBag.websiteTypeList = websiteTypeList;

            var startTime = DateTime.Now.AddHours(-1);
            var data = GetEntitysByLambdaAndSort(o => o.CreatedOn > startTime, OrderLambda, IsASC).ToList();
            return View(data);
        }

        //获取地域分类的信息
        public IActionResult GetNewsForDyKeyword(int num = 20)
        {
            KeywordType2Controller _kt2s = new KeywordType2Controller();
            var dykeyowordList = _kt2s.GetEntitysByLambdaAndSort(o => o.KeywordType1Id == 3,o=>o.SortIndex,true).ToList(); //获取地域关键词
            ViewBag.dykeywordList = dykeyowordList;

            List<InfoHit> infohit = new List<InfoHit>(dykeyowordList.Count * num);
            foreach (KeywordType2 kt2 in dykeyowordList)
            {
                var temp = GetEntitysByLambdaAndSort(o => o.DyKeywordId == kt2.Id, OrderLambda, IsASC).Take(num).ToList();
                infohit.AddRange(temp);
            }
            return View(infohit);
        }

        //获取以网站为分类的信息
        public IActionResult GetWebsiteNews(int num = 10, int? websiteTypeId = 0)
        {
            var websiteList = _ws.GetEntitysByLambdaAndSort(o => o.WebsiteTypeId == websiteTypeId, o => o.SortIndex, false).ToList();
            ViewBag.websiteList = websiteList;

            List<InfoHit> infohit = new List<InfoHit>(websiteList.Count * num);
            foreach (Website website in websiteList)
            {
                var temp = GetEntitysByLambdaAndSort(o => o.WebsiteId == website.Id, OrderLambda, IsASC).Take(num).ToList();
                if (temp.Count > 0) infohit.AddRange(temp);
            }
            return View(infohit);
        }

        //获取同一网站下所有信息
        public IActionResult GetWebsiteJKNews()
        {
            return View();
        }
        #endregion

        public JsonResult GetWebsiteJKNews1(int? pageindex = 1, int? websiteId = 0)
        {
            int pageIndex = pageindex.Value;
            int rowCount = 0, pageCount = 0;
            var data = GetPagedEntitysByLambdaAndSort(pageIndex, PageSize, out rowCount, out pageCount, o => o.WebsiteId == websiteId, OrderLambda, IsASC);

            //Json格式的要求{rowCount:22,pageCount:2,rows:{}},构造成Json的格式传递
            return Json(new
            {
                rowCount = rowCount,
                pageCount = pageCount,
                rows = data
            });
        }
    }
}